#ifndef MacroUtil_h
#define MacroUtil_h

#define ALLOCATE_MEMORY(_ptr, _type, _len) \
        { \
            _ptr = (_type*)calloc(_len, sizeof(_type)); \
            ERROR_ESCAPE(!_ptr, ERROR_OUTOFMEMORY, "AllocArray"); \
        }

#define FREE_MEMORY(_ptr) \
        if (_ptr) \
        { \
            free(_ptr); \
            _ptr = NULL; \
        }

#define NEW_MEMORY(_ptr, _type, _len) \
        { \
            _ptr = new _type[_len]; \
            ERROR_ESCAPE(!_ptr, ERROR_OUTOFMEMORY, "NEW_MEMORY"); \
            memset(_ptr, 0, sizeof(_type) * (_len)); \
        }

#define DELETE_MEMORY(_ptr) \
        if (_ptr) \
        { \
            delete[] (_ptr); \
            _ptr = NULL; \
        }

#define DELETE_OBJECT(_ptr) \
        if (_ptr) \
        { \
            delete (_ptr); \
            _ptr = NULL; \
        }

#define ERROR_ESCAPE(_exp, _err_code, _trace) \
        if (_exp) \
        { \
            DWORD dwErrorCode = _err_code ? _err_code : ::GetLastError(); \
            hResult = HRESULT_FROM_WIN32(dwErrorCode); \
            goto exit; \
        }

#endif // MacroUtil_h